package com.njtech.pet.service.impl;

import com.njtech.pet.constant.Constant;
import com.njtech.pet.dao.ActionDao;
import com.njtech.pet.entity.Action;
import com.njtech.pet.entity.Item;
import com.njtech.pet.entity.User;
import com.njtech.pet.service.ActionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author lxj
 * Date 2020-04-01 17:04
 * Description Action服务实现
 */
@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class ActionServiceImpl implements ActionService {

    @Autowired
    ActionDao actionDao;

    /**
     * 1分钟的毫秒数
     * 十分钟内的浏览记录不会重复添加
     */
    static final int XMIN = 1;
    static final long MSEC = XMIN * 60 * 1000;

    /**
     * 判断某用户是否是今日首次签到，如果有签到过则显示false，如果没签到过则显示true
     * @param uid 用户id
     * @return 结果
     */
    @Override
    public boolean isTodayFirstSignIn(Integer uid) {
        String today = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
        User user = new User();
        user.setId(uid);
        List<Action> actions = actionDao.selectByUserAction(user, Constant.Action.ACTION_SIGN.getValue(),null, Constant.Action.STATE_VALID.getValue(), null,today, null);
        return actions.size() == 0;
    }

    @Override
    public boolean isTodayFirstShare(Integer uid) {
        String today = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
        User user = new User();
        user.setId(uid);
        List<Action> actions = actionDao.selectByUserAction(user, Constant.Action.ACTION_SHARE.getValue(),null, Constant.Action.STATE_VALID.getValue(),null, today, null);
        return actions.size() == 0;
    }

    @Override
    public int addViewAction(Integer uid, Integer itemId) {
        User user = new User();
        user.setId(uid);
        Item item = new Item();
        item.setId(itemId);
        List<Action> actions = actionDao.selectByUserAction(user, Constant.Action.ACTION_VIEW.getValue(),item,Constant.Action.STATE_VALID.getValue(),null,null,null);
        if ( actions != null && actions.size() > 0) {
            //1min内的浏览记录不会重复添加
            Date now = new Date();
            if ( now.getTime() -
                    actions.get(0).getCreateDate().getTime() <= MSEC) {
                //但是会将其时间上调
                Action action = actions.get(0);

                return actionDao.updateByActionId(action);
            }
        }
        Action action = new Action(null, user, item, null, Constant.Action.ACTION_VIEW.getValue(), null, Constant.Action.STATE_VALID.getValue(), null, null);
        return actionDao.insert(action);
    }

    @Override
    public int addCollectAction(Integer uid, Integer itemId) {
        User user = new User();
        user.setId(uid);
        Item item = new Item();
        item.setId(itemId);
        List<Action> actions = actionDao.selectByUserAction(user, Constant.Action.ACTION_COLLECT.getValue(), item, Constant.Action.STATE_VALID.getValue(), null, null,null);
        if ( actions != null && actions.size()>0) {
            //已经有收藏记录了就不用再加了
            return 0;
        }
        Action action = new Action(null, user, item, null, Constant.Action.ACTION_COLLECT.getValue(), null, Constant.Action.STATE_VALID.getValue(), null, null);
        return actionDao.insert(action);
    }

    @Override
    public int addSearchAction(Integer uid, String search) {
        User user = new User();
        user.setId(uid);
        //是否查询过对应记录，如果查询过只需将对应记录的更新日期更换便可以达到前置的目的
        List<Action> actions = actionDao.selectByUserAction(user, Constant.Action.ACTION_VIEW.getValue(), null, Constant.Action.STATE_VALID.getValue(), search, null,null);
        if (actions != null && actions.size() > 0) {
            for (int i = 0;i<actions.size();i++) {
                if (i == 0) {
                    //一条搜索语句只能有一条搜索记录
                    actionDao.updateByActionId(actions.get(0));
                } else {
                    //非法的其他记录应被删除
                    Action action = actions.get(i);
                    action.setState(Constant.Action.STATE_INVALID.getValue());
                    actionDao.updateByActionId(action);
                }
            }
            return 1;
        }
        Action action = new Action();
        action.setUser(user);
        action.setRemark(search);
        action.setState(Constant.Action.STATE_VALID.getValue());
        action.setAction(Constant.Action.ACTION_SEARCH.getValue());
        return actionDao.insert(action);
    }

    @Override
    public int removeAction(Integer collectActionId) {
        Action action = new Action();
        action.setId(collectActionId);
        action.setState(Constant.Action.STATE_INVALID.getValue());
        return actionDao.updateByActionId(action);
    }

    @Override
    public List<Action> getViewListByUserId(Integer uid) {
        User user = new User();
        user.setId(uid);
        return actionDao.selectByUserAction(user, Constant.Action.ACTION_VIEW.getValue(), null, Constant.Action.STATE_VALID.getValue(), null,null, null);
    }

    private List<Item> getItems(List<Action> actions) {
        if (actions == null || actions.size() == 0) {
            return null;
        }
        List<Item> items = new ArrayList<>(actions.size());
        for (Action action : actions) {
            items.add(action.item);
        }
        return items;
    }

    @Override
    public List<Action> getCollectListByUserId(Integer uid) {
        User user = new User();
        user.setId(uid);
        return actionDao.selectByUserAction(user, Constant.Action.ACTION_COLLECT.getValue(), null, Constant.Action.STATE_VALID.getValue(), null,null, null);
    }

    @Override
    public List<String> getSearchListByUserId(Integer uid) {
        User user = new User();
        user.setId(uid);
        List<Action> actions = actionDao.selectByUserAction(user, Constant.Action.ACTION_SEARCH.getValue(), null, Constant.Action.STATE_VALID.getValue(), null, null, null);
        if (actions == null || actions.size() == 0) {
            return null;
        }
        List<String> res = new ArrayList<>(actions.size());
        for (Action action : actions) {
            res.add(action.remark);
        }
        return res;
    }
}
